<?php

namespace App\Http\Controllers\Mini;

use App\Http\Controllers\Home\PaymentController;
use App\Models\Activitys;
use App\Models\AdminAgencys;
use App\Models\Ads;
use App\Models\Adsenses;
use App\Models\Areas;
use App\Models\Categorys;
use App\Models\CompanyInfos;
use App\Models\Configs;
use App\Models\Goods;
use App\Models\QuickDemands;
use App\Models\Regions;
use App\Models\Users;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CommonController extends Controller
{

    /**
     * 获取网站配置信息
     * @route api.public.config
     */
    public function config(Request $request)
    {
        try {
            $base = Configs::getConfig('base');
            return response()->json(["code" => 1, "msg" => "获取成功", "data" => $base]);
        } catch (\Exception $e) {
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }

    /**
     * 获取平台分类
     * @route home.common.getCategorys
     * @param limit 显示条数
     */
    public function getCategorys(Request $request){
        try{
            $lists = Categorys::getCascaderList();//获取分类
            return response()->json([
                "code" => 1,
                "msg" => "获取成功",
                "data" => $lists
            ]);
        }catch (\Exception $e){
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }

    /**
     * 获取微信JSAPI
     * @route api.public.wxJSAPI
     */
    public function wxJSAPI(Request $request){
        try{
            $payment = new PaymentController();
            return $payment->wxJSAPI();
        }catch (\Exception $e){
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }

    /**
     * 广告
     * @route home.common.advs
     * @param limit 显示条数
     * @param 广告位标识 $code
     * WEB_INDEX_BANNER:web-首页banner广告位  WEB_INDEX_CENTER:web-首页中部广告位  WEB_STORE_DETAIL_BANNER:web-供应商首页banner广告位  WEB_STORE_DETAIL_BOTTOM:web_供应商详情底部广告位  WEB_STORE_CATE_TOP:web_供应商分类顶部广告位 WEB_ACT_CATE_TOP:web_活动分类顶部广告位
    WEB_ACT_SIGN_BOTTOM:web-活动报名成功底部广告位  WEB_GOODS_CATE_TOP:web-商品分类顶部广告位  WEB_WORD_CATE_TOP:web-文档分类顶部广告位  WEB_NEWS_LIST_BANNER:web-新闻列表banner广告位 WEB_SEARCH:web-搜索页广告
     * @return bool
     */
    public function advs(Request $request){
        try{
            $params = [
                'code' => (string)$request->post('code'),
                'status' => 1,
            ];
            $limit = (int)$request->post('limit');
            $where['name'] = $params['code'];
            $where['status'] = 1;
            $adsense_id = Adsenses::where($where)->value('id');
            $adsense_id = $adsense_id > 0 ? $adsense_id : 0;
            $data = [
                'adsense_id' => $adsense_id,
                'status' => 1
            ];
            $lists = Ads::getList($data, ['id','adsense_id','title','thumb','link','target'], $limit);
            return response()->json([
                "code" => 1,
                "msg" => "获取成功",
                "data" => $lists
            ]);
        }catch (\Exception $e){
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }

    /**
     * 首页热门活动
     * @route home.common.hotAct
     * @param limit 显示条数
     */
    public function hotAct(Request $request){
        try{
            $fields = ['id','title','thumb','start_at','view','cost','limit','number','cate_pid','cate_id','enroll_at','start_at'];
            $params = [
                'hot' => 2,
                'status' => 1,
            ];
            $limit = (int)$request->post('limit',5);

            $now_time = date('Y-m-d H:i:s');
            $lists = Activitys::allLists($params,$fields, null,$limit);
            foreach ($lists as $K=>$v){
                $lists[$K]['view'] = strlen($v['view'])>=4 ? '999+' : $v['view'];
                $lists[$K]['number'] = strlen($v['number'])>=4 ? '999+' : $v['number'];
                $lists[$K]['start_at'] = trim(@date('Y-m-d', strtotime($v['start_at'])));
                if($v['cost'] == 0){
                    $lists[$K]['cost'] = '免费';
                }else {
                    $lists[$K]['cost'] = $v['cost'];
                }
                if (($v['limit'] == $v['number']) || ($now_time > $v['enroll_at'] && $now_time < $v['start_at'])){
                    $lists[$K]['act_state'] = '报名结束';
                }elseif ($now_time <= $v['enroll_at']){
                    $lists[$K]['act_state'] = '报名中';
                }elseif ($now_time >= $v['start_at']){
                    $lists[$K]['act_state'] = '已举办';
                }
            }
            return response()->json([
                "code" => 1,
                "msg" => "获取成功",
                "data" => $lists
            ]);
        }catch (\Exception $e){
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }

    /**
     * 首页热销商品
     * @route home.common.hotGoods
     * @param limit 显示条数
     */
    public function hotGoods(Request $request){
        try{
            $limit = (int)$request->post('limit',5);
            $lists = Goods::allLists(['hot'=>2], ['id','title','thumb','price','cate_pid','cate_id'], null, $limit);
            return response()->json([
                "code" => 1,
                "msg" => "获取成功",
                "data" => $lists
            ]);
        }catch (\Exception $e){
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }

    /**
     * 公司行业、类型、规模
     * @route home.common.companys
     * @param type 1公司类型、2公司行业、3公司规模、4HR部门人数
     * @return bool
     */
    public function companys(Request $request){
        try{
            $params = [
                'type' => (int)$request->get('type'),
                'status' => 1,
            ];
            $lists = CompanyInfos::getList($params, $fields = ['id','name','type']);
            return response()->json([
                "code" => 1,
                "msg" => "获取成功",
                "data" => $lists
            ]);
        }catch (\Exception $e){
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }

    /**
     * 所有地区
     * @route home.common.regions
     */
    public function regions(Request $request){
        try{
            $lists = Regions::getCascaderList();
            return response()->json([
                "code" => 1,
                "msg" => "获取成功",
                "data" => $lists
            ]);
        }catch (\Exception $e){
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }

    /**
     * 一线城市
     * @route home.common.areas
     */
    public function areas(Request $request)
    {
        try {
            $lists = Areas::where('status','<>',-1)->get(['id', 'name', 'pid'])->toArray();
            return response()->json([
                "code" => 1,
                "msg" => "获取成功",
                "data" => $lists
            ]);
        } catch (\Exception $e) {
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }

    /**
     * HR快速发布需求
     * @route home.common.hrQuickDemand
     * @param user_id 用户id
     * @param mobile 电话号码
     */
    public function hrQuickDemand(Request $request)
    {
        try {
            $user_id = $request->post('user_id',0);//用户id
            if(empty($user_id)) return response()->json(["code"=>0, "msg"=>'请先登录!']);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['username','type','status']);
            if (empty($userinfo)){
                return response()->json(["code"=>0, "msg"=>'用户不存在,请联系客服!']);
            }else{
                if($userinfo['status'] == 2){
                    return response()->json(["code"=>0, "msg"=>'账户正在审核中，请等待审核!']);
                }elseif($userinfo['status'] == 3){
                    return response()->json(["code"=>0, "msg"=>'账号未通过审核，请前往个人中心查看原因并重新提交审核!']);
                }elseif ($userinfo['status'] == 4){
                    return response()->json(["code"=>0, "msg"=>'账号已锁定，请前往个人中心查看原因或联系客服!']);
                }
                if($userinfo['type'] != 1) return response()->json(["code"=>0, "msg"=>'供应商不能评价需求!']);
            }

            //电话号码验证
            $mobile = $request->post('mobile');
            if(!empty($mobile) && !preg_match('/^((13|14|15|16|17|18|19)[0-9]\d{8})$/', $mobile)){
                return response()->json(["code"=>0, "msg"=>'请填写正确的电话号码!']);
            }
            //三分钟内只能点击一次快速发布
            $created_at = QuickDemands::where('user_id',$user_id)->orderBy('created_at','desc')->value('created_at');
            if (!empty($created_at)){
                $created_at->modify('+3 minute');
                if ($created_at > date('Y-m-d H:i:s')){
                    return response()->json(["code"=>0, "msg"=>'三分钟内只能发布一次!']);
                }
            }

            $quick = [];
            $quick['user_id'] = $user_id;
            $quick['user_name'] = $userinfo['username'];
            $quick['mobile'] = $mobile;
            QuickDemands::create($quick);
            //生成未处理数据给后台
            $maps = [
                'title' => '未处理快速发布需求：'.$mobile,
                'type' => 5,
            ];
            AdminAgencys::create($maps);

            return response()->json(["code"=>1, "msg"=>'发布成功,等待平台联系您']);
        } catch (\Exception $e) {
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }

}